/*******************************************************************************
 * Copyright (c) 2010-2011 Robert Fraser                                       *
 *                                                                             *
 * Permission is hereby granted, free of charge, to any person obtaining a     *
 * copy of this software and associated documentation files (the "Software"),  *
 * to deal in the Software without restriction, including without limitation   *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,    *
 * and/or sell copies of the Software, and to permit persons to whom the       *
 * Software is furnished to do so, subject to the following conditions:        *
 *                                                                             *
 *   - The above copyright notice and this permission notice shall be included *
 *     in all copies or substantial portions of the Software.                  *
 *                                                                             *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *
 * DEALINGS IN THE SOFTWARE.                                                   *
 *******************************************************************************/

using System.Windows;
using System.Windows.Controls;
using zeal.util;

namespace zeal.ui.util
{
    internal partial class MeasuringCanvas : Canvas
    {
        protected override Size ArrangeOverride(Size arrangeSize)
        {
            double minX = double.NaN;
            double minY = double.NaN;
            double maxX = double.NaN;
            double maxY = double.NaN;
            foreach(UIElement elem in InternalChildren)
            {
                Size size = elem.DesiredSize;
                double x = GetLeft(elem).zeroIfInvalid();
                double y = GetTop(elem).zeroIfInvalid();
                double w = size.Width.zeroIfInvalid();
                double h = size.Height.zeroIfInvalid();
                elem.Arrange(new Rect(x, y, w, h));
                if(w != 0 || h != 0)
                {
                    minX = nanMin(minX, x);
                    minY = nanMin(minY, y);
                    maxX = nanMax(maxX, x + w);
                    maxY = nanMax(maxY, y + h);
                }
            }
            MinX = minX;
            MinY = minY;
            MaxX = maxX;
            MaxY = maxY;
            //MeasuredWidth = maxX.zeroIfNaN() - minX.zeroIfNaN();
            //MeasuredHeight = maxY.zeroIfNaN() - minY.zeroIfNaN();
            return arrangeSize;
        }

        // These assume b is always valid
        private static double nanMin(double a, double b) { return a.isNaN() ? b : (a < b ? a : b); }
        private static double nanMax(double a, double b) { return a.isNaN() ? b : (a > b ? a : b); }
    }
}